﻿//using System;
//using System.Configuration;
//using System.Xml;
//using System.ComponentModel;
//using System.Reflection;

//namespace mvcForum.Core.Configuration {

//    public class CDataConfigurationElement : ConfigurationElement {
//        private readonly string propertyName;

//        public CDataConfigurationElement() {
//            PropertyInfo[] properties = GetType().GetProperties();
//            int cDataConfigurationPropertyCount = 0;
//            int configurationElementPropertyCount = 0;
//            for (int i = 0; i < properties.Length; i++) {
//                PropertyInfo property = properties[i];
//                ConfigurationPropertyAttribute[] configurationPropertyAttributes = getAttributes<ConfigurationPropertyAttribute>(property);
//                CDataConfigurationPropertyAttribute[] cDataConfigurationPropertyAttribute = getAttributes<CDataConfigurationPropertyAttribute>(property);

//                bool hasConfigurationPropertyAttribute = configurationPropertyAttributes.Length != 0;
//                bool hasCDataConfigurationPropertyAttribute = cDataConfigurationPropertyAttribute.Length != 0;

//                if (hasConfigurationPropertyAttribute && property.PropertyType.IsSubclassOf(typeof(ConfigurationElement))) {
//                    configurationElementPropertyCount++;
//                }

//                if (hasCDataConfigurationPropertyAttribute) {
//                    cDataConfigurationPropertyCount++;
//                    throwIf(cDataConfigurationPropertyCount > 1, "Too many conf elements");
//                    throwIf(!hasConfigurationPropertyAttribute, "Missing conf attribute", property.Name);
//                    throwIf(!property.PropertyType.Equals(typeof(string)), "Must be string", property.Name);
//                    propertyName = configurationPropertyAttributes[0].Name;
//                }
//            }

//            throwIf(configurationElementPropertyCount > 0 && cDataConfigurationPropertyCount > 0, "blah", GetType().FullName);
//        }

//        private T[] getAttributes<T>(PropertyInfo property) where T : Attribute {
//            object[] objectAttributes = property.GetCustomAttributes(typeof(T), true);
//            return Array.ConvertAll<object, T>(objectAttributes,
//                    delegate(object o) {
//                        return o as T;
//                    });
//        }

//        private void throwIf(bool condition, string formatString, params object[] values) {
//            if (condition) {
//                if (values.Length > 0) {
//                    formatString = string.Format(formatString, values);
//                }

//                throw new ConfigurationErrorsException(formatString);
//            }
//        }

//        protected override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey) {
//            bool returnValue;
//            if (string.IsNullOrEmpty(propertyName)) {
//                returnValue = base.SerializeElement(writer, serializeCollectionKey);
//            }
//            else {
//                foreach (ConfigurationProperty configurationProperty in Properties) {
//                    string name = configurationProperty.Name;
//                    TypeConverter converter = configurationProperty.Converter;
//                    string propertyValue = converter.ConvertToString(base[name]);

//                    if (name == propertyName) {
//                        writer.WriteCData(propertyValue);
//                    }
//                    else {
//                        writer.WriteAttributeString("name", propertyValue);
//                    }
//                }
//                returnValue = true;
//            }
//            return returnValue;
//        }

//        protected override void DeserializeElement(XmlReader reader, bool serializeCollectionKey) {
//            if (string.IsNullOrEmpty(propertyName)) {
//                base.DeserializeElement(reader, serializeCollectionKey);
//            }
//            else {
//                foreach (ConfigurationProperty configurationProperty in Properties) {
//                    string name = configurationProperty.Name;
//                    if (name == propertyName) {
//                        string contentString = reader.ReadString();
//                        base[name] = contentString.Trim();
//                    }
//                    else {
//                        string attributeValue = reader.GetAttribute(name);
//                        base[name] = attributeValue;
//                    }
//                }
//                reader.ReadEndElement();
//            }
//        }
//    }
//}